package cn.dhnapplets.service.impl;


import cn.dhnapplets.entity.PackageOrder;
import cn.dhnapplets.entity.TbPackageOrderItem;
import cn.dhnapplets.mapper.TbPackageOrderMapper;
import cn.dhnapplets.service.TbPackageOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Author 邱文彬
 * @Date 2021/11/19 9:59
 * @Version 1.0
 */

@Service("userTbPackageOrderItemServiceImpl")
public class TbPackageOrderItemServiceImpl implements TbPackageOrderItemService {


    @Autowired
    private TbPackageOrderMapper tbPackageOrderMapper;


    @Override
    public PackageOrder getPackageOrderByTime(String openId, String startTime, String finishTime) {
        PackageOrder packageOrder = new PackageOrder();
        Integer orderId = tbPackageOrderMapper.getOrderId(startTime, finishTime, openId);
        if(orderId != 0){
            packageOrder.setOrderQuantity(orderId);
            TbPackageOrderItem tbPackageOrderItem = tbPackageOrderMapper.getHorsemanTodayNo(startTime, finishTime, openId);
            packageOrder.setReclaimedWeight(tbPackageOrderItem.getRecycleWeigh());
            packageOrder.setAmount(tbPackageOrderItem.getSumRecycle());
        }else{
            packageOrder.setOrderQuantity(0);
            packageOrder.setReclaimedWeight(0);
            packageOrder.setAmount(0);
        }

        return packageOrder;

    }

    @Override
    public PackageOrder getPackageOrderThisYear(String openId) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = formatter.format(getDayBegin(-365));
        String finishTime = formatter.format(getDayEnd());
        PackageOrder packageOrder = new PackageOrder();
        Integer orderId = tbPackageOrderMapper.getOrderId(startTime, finishTime, openId);
        if(orderId != 0){
            packageOrder.setOrderQuantity(orderId);
            TbPackageOrderItem tbPackageOrderItem = tbPackageOrderMapper.getHorsemanTodayNo(startTime, finishTime, openId);
            packageOrder.setReclaimedWeight(tbPackageOrderItem.getRecycleWeigh());
            packageOrder.setAmount(tbPackageOrderItem.getSumRecycle());
        }else{
            packageOrder.setOrderQuantity(0);
            packageOrder.setReclaimedWeight(0);
            packageOrder.setAmount(0);
        }
        return packageOrder;

    }

    @Override
    public PackageOrder getPackageOrderThisMonth(String openId) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = formatter.format(getDayBegin(-30));
        String finishTime = formatter.format(getDayEnd());
        PackageOrder packageOrder = new PackageOrder();
        Integer orderId = tbPackageOrderMapper.getOrderId(startTime, finishTime, openId);
        if(orderId != 0){
            packageOrder.setOrderQuantity(orderId);
            TbPackageOrderItem tbPackageOrderItem = tbPackageOrderMapper.getHorsemanTodayNo(startTime, finishTime, openId);
            packageOrder.setReclaimedWeight(tbPackageOrderItem.getRecycleWeigh());
            packageOrder.setAmount(tbPackageOrderItem.getSumRecycle());
        }else{
            packageOrder.setOrderQuantity(0);
            packageOrder.setReclaimedWeight(0);
            packageOrder.setAmount(0);
        }
        return packageOrder;
    }

    @Override
    public PackageOrder getPackageOrderThisWeek(String openId) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = formatter.format(getDayBegin(-7));
        String finishTime = formatter.format(getDayEnd());
        PackageOrder packageOrder = new PackageOrder();
        Integer orderId = tbPackageOrderMapper.getOrderId(startTime, finishTime, openId);
        if(orderId != 0){
            packageOrder.setOrderQuantity(orderId);
            TbPackageOrderItem tbPackageOrderItem = tbPackageOrderMapper.getHorsemanTodayNo(startTime, finishTime, openId);
            packageOrder.setReclaimedWeight(tbPackageOrderItem.getRecycleWeigh());
            packageOrder.setAmount(tbPackageOrderItem.getSumRecycle());
        }else{
            packageOrder.setOrderQuantity(0);
            packageOrder.setReclaimedWeight(0);
            packageOrder.setAmount(0);
        }

        return packageOrder;
    }

    @Override
    public PackageOrder getPackageOrderToday(String openId) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = formatter.format(getDayBegin(-0));
        String finishTime = formatter.format(getDayEnd());
        PackageOrder packageOrder = new PackageOrder();
        Integer orderId = tbPackageOrderMapper.getOrderId(startTime, finishTime, openId);
        if(orderId != 0){
            packageOrder.setOrderQuantity(orderId);
            TbPackageOrderItem tbPackageOrderItem = tbPackageOrderMapper.getHorsemanTodayNo(startTime, finishTime, openId);
            packageOrder.setReclaimedWeight(tbPackageOrderItem.getRecycleWeigh());
            packageOrder.setAmount(tbPackageOrderItem.getSumRecycle());
        }else{
            packageOrder.setOrderQuantity(0);
            packageOrder.setReclaimedWeight(0);
            packageOrder.setAmount(0);
        }

        return packageOrder;
    }


    public static Date getDayBegin(int num) {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(cal.DATE, num);
        return cal.getTime();
    }

    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
}
